<?php

namespace App\Http\Controllers\Back;

use App\Model\InterPushMatchUser;
use App\Model\InterPushUserInfo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class InterPushController extends Controller
{
    public function report()
    {
        $builder = DB::table('interpush_report');
        if (\request()->has('user_id')){
            $builder->where('user_id',\request('user_id'));
        }
        if (\request()->has('type')){
            $builder->where('type', \request('yupe'));
        }

        if (\request()->has('status')){
            $builder->where('status', \request('status'));
        }

        if (request()->has('time_start')){
            $builder->where('created_at', '>=', request('time_start'));
        }
        if (request()->has('time_end')){
            $builder->where('created_at', '<=', request('time_end'));
        }
        if (request()->has('time_start') and request()->has('time_end')){
            $builder->whereBetween('created_at', [request('time_start'), request('time_end')]);
        }
        $reports = $builder->orderByDesc('created_at')->paginate();

        return $this->renderJson($reports->toArray());
    }

    /**
     * 更改处理状态
     */
    public function report_already()
    {
        $this->validate(\request(), [
            'id' => 'required'
        ]);

        $id = \request('id');

        $r = DB::table('interpush_report')->where('id',$id)->update(['status' => 1]);
        if ($r){
            return $this->success();
        }
        return $this->error();
    }

    /**
     * 获取举报分享内容
     */
    public function report_share_content()
    {
        $this->validate(\request(), [
            'id' => 'required'
        ]);

        $report = DB::table('interpush_report')->where('id', request('id'))->first();
        if ($report == null){
            return $this->fail('没有数据');
        }

        $msg = InterPushMatchUser::where('user_id',$report->user_id)->where('match_user_id',$report->match_id)->first();
        return $this->renderJson([
            'type' => $msg->match_interpush_type,
            'article_id' => $msg->match_interpush_article
        ]);
    }

    public function report_wechat()
    {
        $this->validate(\request(), [
            'id' => 'required'
        ]);

        $report = DB::table('interpush_report')->where('id', request('id'))->first();
        if ($report == null){
            return $this->fail('没有数据');
        }
        $info = InterPushUserInfo::where('user_id', $report->match_id)->first();
        return $this->renderJson([
            'qrcode' => $info->weixin_img
        ]);
    }
}
